package com.cskaoyan.controller.wxorder;

import com.cskaoyan.bean.MallOrderGoods;
import com.cskaoyan.model.bo.order.CommentBO;
import com.cskaoyan.model.bo.wxcart.CartCheckoutIdBo;
import com.cskaoyan.model.bo.wxorder.WxOrderBO;
import com.cskaoyan.model.bo.wxorder.WxOrderIdBO;
import com.cskaoyan.model.bo.wxorder.WxSubmitBO;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.wxcart.WxCheckoutTotalVo;
import com.cskaoyan.model.vo.wxorder.WxSubmitVO;
import com.cskaoyan.model.vo.wxorder.detail.DetailVO;
import com.cskaoyan.model.vo.wxorder.list.OrderListVO;
import com.cskaoyan.service.wxcart.WxCartService;
import com.cskaoyan.service.wxorder.WxOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @program: project-market
 * @description: wxOrder
 * @author: shiyue
 * @create: 2021/12/2 14:48
 **/
@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    @Autowired
    WxCartService cartService;

    @RequestMapping("list")
    public BaseRespVo getlist(WxOrderBO bo) {
        OrderListVO list = wxOrderService.getlist(bo);
        return BaseRespVo.ok(list);
    }

    @RequestMapping("detail")
    public BaseRespVo getDetail(Integer orderId) {
        DetailVO detailVO = wxOrderService.getDetail(orderId);
        return BaseRespVo.ok(detailVO);
    }

    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody WxOrderIdBO bo) {
        int row = wxOrderService.cancel(bo);
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo deleteOrder(@RequestBody WxOrderIdBO bo) {
        int row = wxOrderService.deleteOrder(bo);
        return BaseRespVo.ok();
    }

    @RequestMapping("confirm")
    public BaseRespVo confirmOrder(@RequestBody WxOrderIdBO bo) {
        // System.out.println("test1");
        int row = wxOrderService.confirmOrder(bo);
        return BaseRespVo.ok();
    }

    @RequestMapping("refund")
    public BaseRespVo refundOrder(@RequestBody WxOrderIdBO bo) {
        int row = wxOrderService.refundOrder(bo);
        return BaseRespVo.ok();
    }


    @RequestMapping("goods")
    public BaseRespVo goodsWhenComment(Integer orderId, Integer goodsId) {
        MallOrderGoods mallOrderGoods = wxOrderService.goodsWhenComment(orderId, goodsId);
        return BaseRespVo.ok(mallOrderGoods);
    }

    @RequestMapping("comment")//每评论一次要把market_order里的comments减1
    public BaseRespVo commentOrder(@RequestBody CommentBO commentBO) {
        int row = wxOrderService.commentOrder(commentBO);
        return BaseRespVo.ok();
    }

    @PostMapping("submit")
    public BaseRespVo commentOrder(HttpSession session, @RequestBody WxSubmitBO bo) {

        // 以cart/checkout取得购物车和商品数据,WxSubmitBO转CartCheckoutIdBo
        CartCheckoutIdBo cartCheckoutIdBo = new CartCheckoutIdBo(bo.getCartId(), bo.getAddressId(), bo.getCouponId(), bo.getUserCouponId(), bo.getGrouponRulesId());

        Integer uid = (Integer) session.getAttribute("uid");

        if (uid == null) return BaseRespVo.manual(501, "请登录", null);

        WxCheckoutTotalVo vo = cartService.checkout(cartCheckoutIdBo, uid);

        WxSubmitVO wxSubmitVO = wxOrderService.submitOrder(vo, bo.getMessage());

        return BaseRespVo.ok(wxSubmitVO);
    }


    @PostMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map map) {
        return BaseRespVo.manual(724, "订单不能支付", null);
    }


}
